package com.tao.auto.batchtao.home;

import com.tao.auto.batchtao.base.ParamContext;
import com.tao.auto.batchtao.base.accout.BaseAccount;
import com.tao.auto.batchtao.database.dao.MgtUserInfoRepository;
import com.tao.auto.batchtao.database.entity.MgtUserInfo;
import com.tao.auto.batchtao.home.account.HomeAccount;
import com.tao.auto.batchtao.home.account.LoginToken;
import com.tao.auto.batchtao.home.account.UserMgt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Example;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author GLNC-taowenchen
 */
@Component
public class HomeLoginBaseFlowImpl extends AbstractHomeLoginBaseFlow {

    private BaseAccount<HomeAccount> homeLoginAccountImpl;
    private RestTemplate restTemplate;
    private MgtUserInfoRepository mgtUserInfoRepository;

    public HomeLoginBaseFlowImpl(@Qualifier("homeLoginAccountImpl") BaseAccount<HomeAccount> homeLoginAccountImpl
            , RestTemplate restTemplate
            , MgtUserInfoRepository mgtUserInfoRepository) {
        this.homeLoginAccountImpl = homeLoginAccountImpl;
        this.restTemplate = restTemplate;
        this.mgtUserInfoRepository = mgtUserInfoRepository;
    }

    @Override
    protected void qiandao(ParamContext<String> login, ParamContext<HomeAccount> phone) {
        if (login == null) {
            return;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authori-zation", "Bearer " + login.getData());
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = "http://cr.dongweicom.com/api/sign/integral";
        Map<String, String> params = new HashMap<>(8);
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<String> loginTokenResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        System.out.println(loginTokenResponseEntity);
        after(login, phone);
    }

    private void after(ParamContext<String> login, ParamContext<HomeAccount> phone) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authori-zation", "Bearer " + login.getData());
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = "http://cr.dongweicom.com/api/user";
        Map<String, String> params = new HashMap<>(8);
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<UserMgt> loginTokenResponseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, UserMgt.class);
        //更新数据库

        Optional<MgtUserInfo> one = mgtUserInfoRepository.findMgtUserInfoByUsername(phone.getData().getAccount());
        one.ifPresent(mgtUserInfo1 -> {
            mgtUserInfo1.setNumber((long) Double.parseDouble(loginTokenResponseEntity.getBody().getData().getIntegral()));
            mgtUserInfo1.setInvitationCode(loginTokenResponseEntity.getBody().getData().getUid());
            mgtUserInfo1.setSignTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            mgtUserInfoRepository.save(mgtUserInfo1);
        });

    }

    @Override
    public ParamContext<String> login(ParamContext<HomeAccount> phone) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String url = "http://cr.dongweicom.com/api/login";
        Map<String, String> params = new HashMap<>(8);
        params.put("account", phone.getData().getAccount());
        params.put("password", phone.getData().getPassword());
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<LoginToken> loginTokenResponseEntity = restTemplate.postForEntity(url, entity, LoginToken.class);
        System.out.println(loginTokenResponseEntity);
        if (loginTokenResponseEntity.getBody()==null || loginTokenResponseEntity.getBody().getData() == null) {
            Optional<MgtUserInfo> one = mgtUserInfoRepository.findMgtUserInfoByUsername(phone.getData().getAccount());
            one.ifPresent(mgtUserInfo1 -> mgtUserInfoRepository.delete(mgtUserInfo1));
            return null;
        }
        return new ParamContext<String>().paramContext(loginTokenResponseEntity.getBody().getData().getToken());
    }

    @Autowired
    private AbstractHomeRegisterBaseFlow abstractHomeRegisterBaseFlow;

    @Override
    protected ParamContext<HomeAccount> getAccount() {
        return new ParamContext<HomeAccount>().paramContext(homeLoginAccountImpl.account());
    }
}
